<?php
class sitesControl extends skymvc{
	
	public function __construct(){
		parent::__construct();	
	}
	
	public function onDefault(){
		if(ADMINGID>2){
			$this->onAdd();
		}
		$where=$GLOBALS['sw'];
		$where.=" AND status<10 ";
		$keyword=get('keyword','h');
		if($keyword){
			$where.=" AND sitename like '%".$keyword."%' ";
		}
		$limit=100;
		$start=get('per_page','i');
		$option=array(
			"where"=>$where,
			"limit"=>$limit,
			"start"=>$start,
			"order"=>" siteid DESC"
		);
		$rscount=true;
		$list=M("sites")->select($option,$rscount);
		
		$pagelist=$this->pagelist($rscount,$limit,APPADMIN."?m=sites");
		$this->smarty->assign(
			array(
				"data"=>$list, 
				"pagelist"=>$pagelist
			)
		);
		$this->smarty->display("sites/index.html");
	}
	
	public function onAdd(){
		$siteid=get_post('siteid','i');
		if(ADMINGID>2){
			$siteid=SITEID;
		}
		
		if($siteid){
			$this->smarty->assign("data",M("sites")->selectRow(array("where"=>array("siteid"=>$siteid))));
		}
		 
		$this->smarty->display("sites/add.html");
	}
	
	public function onSave(){
		$siteid=intval(get_post('siteid','i'));
		$data=M('sites')->postData();
		$data['sitename']=post('sitename','h');
		$data['domain']=post('domain','h');
		$data['is_open']=post('is_open','i');
		$data['title']=post('title','h');
		$data['keywords']=post('keywords','h');
		$data['description']=post('description','h');
		if(empty($data['description'])){			
			$data['description']=cutstr(strip_tags($_POST['content']),240);
		}
		$data['close_why']=post('close_why','h');
		$data['logo']=post('logo','h');
		$data['icp']=post('icp','h');
		$data['index_template']=post('index_template','h');
		$data['statjs']=$_POST['statjs'];
		$data['wapbg']=post('wapbg','h');
		if($siteid){
			M("sites")->update($data,"siteid=".$siteid);
		}else{
			$sid=M("sites")->insert($data);
			M("koudai")->insert(array(
					"siteid"=>$sid,
					"dateline"=>time(),
					"status"=>1,
					"title"=>"口袋店"
				));
		}
		
		$this->goall($this->lang['edit_success']);
	}
	
	public function onwriteconfig(){
		$data=M("sites")->select();
		$str="<?php\r\n";
		if($data){
		foreach($data as $k=>$v){
			if($k==0){
				$str.='$sites["default"]="'.$v["domain"].'";'."\r\n";
				$str.='$sites["'.str_replace(".","_",$v['domain']).'"]='.$v['siteid'].';'."\r\n";
			}else{
				$str.='$sites["'.str_replace(".","_",$v['domain']).'"]='.$v['siteid'].';'."\r\n";
			}
		}
		$str.="?>";
		}
		file_put_contents("config/sites.php",$str);
		$this->goall($this->lang['operation_success']);
	}
	
	public function onDelete(){
		$siteid=get('siteid','i');
		M("sites")->update(array("status"=>99),"siteid=".$siteid);
		$this->goAll("删除成功");
	}
 
	
	
}

?>